package com.gupaoedu.vip;

import com.gupaoedu.vip.registry.IRegistryCenter;
import com.gupaoedu.vip.registry.RegistryCenterWithZk;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author: JLL
 * @Date: 2019/6/18 17:10
 */
@Component
public class GpRpcServer implements ApplicationContextAware , InitializingBean {

    private int port;

    public GpRpcServer(int port) {
        this.port = port;
    }

    private IRegistryCenter iRegistryCenter=new RegistryCenterWithZk();


    private ExecutorService executorService = Executors.newCachedThreadPool();
    private Map<String,Object> handlerMap = new HashMap();

    /**
     * 设置属性之后做的事。。
     * @throws Exception
     */
    @Override
    public void afterPropertiesSet() throws Exception {
        ServerSocket serverSocket =  null;
        Socket socket = null;
        try {
            serverSocket = new ServerSocket(port);
            while (true){ //不断接收请求
                socket = serverSocket.accept(); //BIO
//                ProcessorHandler command  =
                executorService.execute(new ProcessorHandler(socket,handlerMap));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                socket.close();
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, Object> serviceBeanMap =  applicationContext.getBeansWithAnnotation(RpcService.class);
        if(!serviceBeanMap.isEmpty()){
            for (Object serviceBean : serviceBeanMap.values()) {
                //拿到注解
                RpcService rpcService = serviceBean.getClass().getAnnotation(RpcService.class);
                String serviceName = rpcService.value().getName(); //拿到接口、注解的value值是这个：om.gupaoedu.vip.IHelloService
                String version =rpcService.version();
                if(!StringUtils.isEmpty(version)){
                    serviceName += "-" + version;
                }
                handlerMap.put(serviceName,serviceBean);
                iRegistryCenter.registry(serviceName,getAddress()+":"+port);
            }
        }

    }

    private static String getAddress(){
        InetAddress inetAddress=null;
        try {
            inetAddress=InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return inetAddress.getHostAddress();// 获得本机的ip地址
    }

}
